//
//  AuthenService.m
//  HxgProject
//
//  Created by hxg on 2018/3/27.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "AuthenService.h"
#import "NetworkManager.h"

#import "ContactInfo.h"
#import "ReputationDetail.h"
#import "ResObject.h"
#import "PersonInfo.h"

@implementation AuthenService

- (void)getUserInfoWithCallBack:(void(^)(PersonInfo *person)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@",@"http://47.104.139.178:8080/demo/superadmin/GetUserInfo"];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    
    [man getURL:urlInfo parameters:params success:^(id responseObject) {
        
        NSLog(@"userInfo ===>%@",responseObject);
        
        NSDictionary *dic = [responseObject objectForKey:@"userInfo"];
        
        PersonInfo *personInfo = [[PersonInfo alloc] init];
        if ([responseObject objectForKey:@"userInfo"] != [NSNull null]
            && nil != dic)
        {
            personInfo.name = dic[@"name"];
            personInfo.school = dic[@"school"];
            personInfo.education = dic[@"education"];
            personInfo.schoolAddr = dic[@"schoolAddr"];
            personInfo.qq = dic[@"qq"];
            
            personInfo.webchat = dic[@"webchat"];
            personInfo.idCard = dic[@"IDCard"];
            personInfo.bankAccount = dic[@"bankAccount"];
            personInfo.bank = dic[@"bank"];
            personInfo.subBank = dic[@"subBank"];
            
            personInfo.str = dic[@"str"];
            personInfo.chsiAcc = dic[@"chsiAccount"];
            personInfo.chsiPwd = dic[@"chsiPwd"];
            personInfo.frontImg = dic[@"frontImg"];
            personInfo.flankImg = dic[@"flankImg"];

            personInfo.bodyImg = dic[@"bodyImg"];
            personInfo.phone = dic[@"phone"];
        }
        
        
        callBack(personInfo);
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        callBack(nil);
    }];
}


- (void)setUserInfoWithPerson:(PersonInfo *)person
                withCallBack:(void(^)(BOOL isSuccess))callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@",@"http://47.104.139.178:8080/demo/superadmin/SaveUserInfo"];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    params[@"name"] = person.name;
    params[@"school"] = person.school;
    params[@"education"] = person.education;
    params[@"schoolAddr"] = person.schoolAddr;
    
    params[@"qq"] = person.qq;
    params[@"webchat"] = person.webchat;
    params[@"idCard"] = person.idCard;
    params[@"bankAccount"] = person.bankAccount;
    params[@"bank"] = person.bank;

    params[@"subBank"] = person.subBank;
    params[@"chsiAccount"] = person.chsiAcc;
    params[@"chsiPwd"] = person.chsiPwd;
    params[@"frontImg"] = person.frontImg;
    params[@"flankImg"] = person.flankImg;
    params[@"bodyImg"] = person.bodyImg;
    
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        if ([[responseObject objectForKey:@"Success"]  isEqual: @(YES)])
        {
            callBack(YES);

        }
        else
        {
            callBack(NO);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        callBack(NO);
    }];
}

- (void)saveContacts:(ContactInfo *)contactInfo
        withCallBack:(void(^)(ResObject *res)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@",@"http://47.104.139.178:8080/demo/contacts/SaveContacts"];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];

    params[@"contactName1"] = contactInfo.contactName1;
    params[@"contactPhone1"] = contactInfo.contactPhone1;
    params[@"relation1"] = contactInfo.relation1;
    
    params[@"contactName2"] = contactInfo.contactName2;
    params[@"contactPhone2"] = contactInfo.contactPhone2;
    params[@"relation2"] = contactInfo.relation2;
    params[@"vendorPwd"] = contactInfo.vendorPwd;

    ResObject *myRes = [[ResObject alloc] init];
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        
        if ([[responseObject objectForKey:kSuccess]  isEqual: @(YES)])
        {
            myRes.isSuccess = YES;
            callBack(myRes);
        }
        else
        {
            myRes.isSuccess = NO;
            myRes.errMsg = [responseObject objectForKey:@"errMsg"];
            callBack(myRes);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        myRes.isSuccess = NO;
        myRes.errMsg = @"操作失败!";
        callBack(myRes);
    }];
}

- (void)getContactsWithCallBack:(void(^)(ContactInfo *info)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@",@"http://47.104.139.178:8080/demo/contacts/GetContacts"];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    
    [man getURL:urlInfo
     parameters:params
        success:^(id responseObject) {
            NSLog(@"responseObject===/////=:%@",responseObject);
            
            NSDictionary *dic = [responseObject objectForKey:@"userContacts"];
            ContactInfo *contactInfo = [[ContactInfo alloc] init];
            if ([responseObject objectForKey:@"userContacts"] != [NSNull null]
                && nil != dic)
            {
                contactInfo.contactName1 = dic[@"contactName1"];
                contactInfo.contactPhone1 = dic[@"contactPhone1"];
                contactInfo.relation1 = dic[@"relation1"];
                
                contactInfo.contactName2 = dic[@"contactName2"];
                contactInfo.contactPhone2 = dic[@"contactPhone2"];
                contactInfo.relation2 = dic[@"relation2"];
                
                NSString *vendorPwdStr = @"";
                if (isEmptyString(dic[@"vendorPwd"]))
                {
                    vendorPwdStr = @"";
                }
                else
                {
                    vendorPwdStr = dic[@"vendorPwd"];
                }
                contactInfo.vendorPwd = vendorPwdStr;
                contactInfo.idCard = dic[@"idCard"];
            }
            
            callBack(contactInfo);
        } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
            NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
            callBack(nil);
        }];
}

- (void)saveReputationDetail:(ReputationDetail *)reputationDetail
                withCallBack:(void(^)(BOOL isSuccess)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@",@"http://47.104.139.178:8080/demo/ReputationDetail/SaveReputationDetail"];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    NSLog(@"phoneNum===>%@",[[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum]);
    
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    
    params[@"alipayAccount"] = reputationDetail.alipayAccount;
    params[@"alipayPwd"] = @"";
    params[@"jdAccount"] = reputationDetail.jdAccount;
        params[@"jdPwd"] = @"";
    
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        NSLog(@"xin yu ==>responseObject:%@",responseObject);
        
        if ([[responseObject objectForKey:@"Success"]  isEqual: @(YES)])
        {
            callBack(YES);
        }
        else
        {
            callBack(NO);
        }
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        callBack(NO);
    }];
}

- (void)getReputationDetailWithCallBack:(void(^)(ReputationDetail *rep)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@",@"http://47.104.139.178:8080/demo/ReputationDetail/GetReputationDetail"];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"phone"] = [[NSUserDefaults standardUserDefaults] objectForKey:kPhoneNum];
    
    [man getURL:urlInfo
     parameters:params
        success:^(id responseObject) {
            NSLog(@"ReputationDetail<<==>:%@",responseObject);
            
            NSDictionary *dic = [responseObject objectForKey:@"ReputaTionDailInfo"];
            ReputationDetail *reputation = [[ReputationDetail alloc] init];
            if ([responseObject objectForKey:@"ReputaTionDailInfo"] != [NSNull null]
                && nil != dic)
            {
                reputation.alipayAccount = dic[@"alipayAccount"];
                reputation.alipayPwd = dic[@"alipayPwd"];
                reputation.jdAccount = dic[@"jdAccount"];
                reputation.jdPwd = dic[@"jdPwd"];
            }
          
            callBack(reputation);
        } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
            NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
            callBack(nil);
        }];
}



@end
